/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.adapter.MeiRongAdapter;
import com.gmail.blackdog1987.carcare.entity.TMaintain;
import com.gmail.blackdog1987.carcare.entity.base.DataPage;
import com.gmail.blackdog1987.carcare.network.request.builder.MeirongRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.CleanShopResponse;
import com.gmail.blackdog1987.carcare.network.response.MeirongResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.MeirongResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class MeiRongActivity extends BaseActivity implements OnResponseListener,
		OnClickListener, OnLastItemVisibleListener,
		OnRefreshListener2<ListView>, OnItemClickListener {
	private Context mContext;
	private PullToRefreshListView mListView;
	private ListView mActualListView;
	private TextView mWeihu;
	private TextView mBaoyang;
	private TextView mMeirong;
	private MeiRongAdapter mAdapter;
	private MeirongRequestBuilder mReq = new MeirongRequestBuilder();
	private MeirongResponseHandler mHandler = new MeirongResponseHandler();
	private int type =1;
	private DataPage<TMaintain> page;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_meirong);
		mContext = this;

		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		mWeihu = (TextView) findViewById(R.id.maintain_weihu);
		mBaoyang = (TextView) findViewById(R.id.maintain_baoyang);
		mMeirong = (TextView) findViewById(R.id.maintain_meirong);
		mListView = (PullToRefreshListView) findViewById(R.id.maintain_list);
		mListView.setMode(Mode.BOTH);
		mActualListView = mListView.getRefreshableView();
	}

	public void reg() {
		mListView.setOnRefreshListener(this);
		mListView.setOnLastItemVisibleListener(this);
		mActualListView.setOnItemClickListener(this);
		mHandler.setOnResponseListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		mWeihu.setOnClickListener(this);
		mBaoyang.setOnClickListener(this);
		mMeirong.setOnClickListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		mAdapter = new MeiRongAdapter(mContext, null);
		mActualListView.setAdapter(mAdapter);
		mReq = new MeirongRequestBuilder();
		mReq.setLat(0.0)
			.setLon(0.0)
			.setPageIndex(1)
			.setPageSize(20)
			.setType(type)
			.postJsonRequest(mContext, mHandler);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.maintain_weihu:
			type = 1;
			mWeihu.setTextColor(getResources().getColor(R.color.white));
			mBaoyang.setTextColor(getResources().getColor(R.color.green));
			mMeirong.setTextColor(getResources().getColor(R.color.green));
			mWeihu.setBackgroundResource(R.drawable.bg_tx_left_pressed);
			mBaoyang.setBackgroundResource(R.drawable.bg_tx_center);
			mMeirong.setBackgroundResource(R.drawable.bg_tx_right);
			mReq.setLat(0.0)
				.setLon(0.0)
				.setPageIndex(1)
				.setPageSize(20)
				.setType(type)
				.postJsonRequest(mContext, mHandler);
			break;
		case R.id.maintain_baoyang:
			type = 2;
			mWeihu.setTextColor(getResources().getColor(R.color.green));
			mBaoyang.setTextColor(getResources().getColor(R.color.white));
			mMeirong.setTextColor(getResources().getColor(R.color.green));
			mWeihu.setBackgroundResource(R.drawable.bg_tx_left);
			mBaoyang.setBackgroundResource(R.drawable.bg_tx_center_pressed);
			mMeirong.setBackgroundResource(R.drawable.bg_tx_right);
			mReq.setLat(0.0)
				.setLon(0.0)
				.setPageIndex(1)
				.setPageSize(20)
				.setType(type)
				.postJsonRequest(mContext, mHandler);
			break;
		case R.id.maintain_meirong:
			type = 3;
			mWeihu.setTextColor(getResources().getColor(R.color.green));
			mBaoyang.setTextColor(getResources().getColor(R.color.green));
			mMeirong.setTextColor(getResources().getColor(R.color.white));
			mWeihu.setBackgroundResource(R.drawable.bg_tx_left);
			mBaoyang.setBackgroundResource(R.drawable.bg_tx_center);
			mMeirong.setBackgroundResource(R.drawable.bg_tx_right_pressed);
			mReq.setLat(0.0)
				.setLon(0.0)
				.setPageIndex(1)
				.setPageSize(20)
				.setType(type)
				.postJsonRequest(mContext, mHandler);
			break;
		default:
			break;
		
		}

	}

	@Override
	public void onResponse(IResponseHandler classz) {
		// insert db
		page = ((MeirongResponse)classz.getResponse()).datas;
		if(page.currentPageNo <= 1){
			mAdapter.setDatas(page.datas);
		}else{
			mAdapter.addDatas(page.datas);
			mAdapter.notifyDataSetChanged();
		}
		mListView.onRefreshComplete();
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "加载失败", Toast.LENGTH_LONG).show();
		mListView.onRefreshComplete();
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> arg0) {
		mReq.setLat(0.0)
			.setLon(0.0)
			.setPageIndex(1)
			.setPageSize(20)
			.postJsonRequest(mContext, mHandler);
}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> arg0) {
		if(page==null){
			mReq.setLat(0.0)
				.setType(type)
				.setLon(0.0)
				.setPageIndex(1)
				.setPageSize(20)
				.postJsonRequest(mContext, mHandler);
		}else if(page.currentPageNo >= page.lastPageNo){
			Toast.makeText(mContext, "没有了",Toast.LENGTH_LONG).show();
			mListView.onRefreshComplete();
		}else{
			mReq.setLat(0.0)
				.setType(type)
				.setLon(0.0)
				.setPageIndex(page.nextPage)
				.setPageSize(page.pageSize)
				.postJsonRequest(mContext, mHandler);
		}
	}

	@Override
	public void onLastItemVisible() {

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent i = new Intent(MeiRongActivity.this, OrderMeiRongActivity.class);
		i.putExtra("data", mAdapter.getItem(position-1));
		startActivity(i);
	}
}
